/**
 * Copyright 2008 blacky
 * The XPCOM module that represents a html convertor for 
 * filtering and patching of js code from combats.com domain.
 */
 
const CONVERSION = "?from=text/com.combats.html&to=*/*";
const CONTRACT_ID = "@mozilla.org/streamconv;1" + CONVERSION;
const CLASS_ID = Components.ID("{f42e6d8e-1df8-4298-b938-82cdf6bf9c56}");
var componentFile = __LOCATION__;
var componentsDir = componentFile.parent;
var extensionDir = componentsDir.parent;
var chromeDir = extensionDir.clone();
chromeDir.append('chrome');
var contentDir = chromeDir.clone();
contentDir.append('content');

var aConsoleService;
var aPrefService;
var debugMsg =function(msg) 
{
    msg = 'Combats Convertor:: ' + msg;
    if (!aPrefService)
    {
      aConsoleService = Components.classes["@mozilla.org/consoleservice;1"].
      getService(Components.interfaces.nsIConsoleService);

      aPrefService = Components.classes["@mozilla.org/preferences-service;1"].
      getService(Components.interfaces.nsIPrefService).getBranch('extensions.uriid.'); 
     
      try
      {
        var js = aPrefService.getBoolPref('showInConsole');
      }
      catch(e)
      {
        aPrefService.setBoolPref('showInConsole', true);
      }
    }
    if(aPrefService.getBoolPref('showInConsole'))
    {
      aConsoleService.logStringMessage(msg);
    }
    dump(msg);
}

//class constructor
function CombatsStreamConvertor() {
 
};

CombatsStreamConvertor.prototype.QueryInterface = function(iid) { 
  
  //debugMsg("QueryInterface enter");
  
         if (iid.equals(Components.interfaces.nsISupports) 
                         || iid.equals(Components.interfaces.nsIStreamConverter) 
                         || iid.equals(Components.interfaces.nsIStreamListener) 
                         || iid.equals(Components.interfaces.nsIRequestObserver)) 
                 return this; 
  
         throw Components.results.NS_ERROR_NO_INTERFACE; 
  
}; 

  
// nsIRequestObserver methods 
CombatsStreamConvertor.prototype.onStartRequest = function(aRequest, aContext) { 
  //debugMsg("onStartRequest enter");

         this.data = ''; 
         this.uri = aRequest.QueryInterface(Components.interfaces.nsIChannel).URI.spec; 
         //this.charset = aRequest.QueryInterface(Components.interfaces.nsIChannel).contentCharset; 
         this.channel = aRequest; 
         this.channel.contentType = "text/html"; 
         this.listener.onStartRequest(this.channel, aContext); 
};

//CombatsRegExpFix class
function CombatsRegExpFix(aRegexp, aReplacement) {
  this.regexp = aRegexp;
  this.replacement = aReplacement;
};

CombatsRegExpFix.prototype.apply = function(data)
{
  return data.replace(this.regexp, this.replacement);
};

//CombatsJsIncludeFix class
function CombatsJsIncludeFix(aSrcUrl, aFileName)
{
var file;
try
{
  var regexpStr = "<SCRIPT[^>]+src\s*=\s*[\'\"]" + aSrcUrl + "[^>]*>\s*</SCRIPT>";
  this.regexp = new RegExp(regexpStr, "gi");
  file = contentDir.clone();
  file.append(aFileName);
  this.fileContent = "";
  var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"]
	.createInstance(Components.interfaces.nsIFileInputStream);
  var sstream = Components.classes["@mozilla.org/scriptableinputstream;1"]
    .createInstance(Components.interfaces.nsIScriptableInputStream);
  fstream.init(file, -1, 0, 0);
  sstream.init(fstream); 
  var str = sstream.read(4096);
  while (str.length > 0) {
    this.fileContent += str;
    str = sstream.read(4096);
  }
  sstream.close();
  fstream.close();
  this.fileContent = "<SCRIPT language=\"javascript\">\r\n" + this.fileContent + "\r\n</SCRIPT>\r\n";
}
catch (e)
{
debugMsg(e);
if (file)
  debugMsg(file.path);
}
}

CombatsJsIncludeFix.prototype.apply = function(data)
{
  if (data.match(this.regexp))
  {
    var ret = data.replace(this.regexp, this.fileContent);
    return ret;
  }
  return data;
};

//CombatsCssIncludeFix class
function CombatsCssIncludeFix(aSrcUrl, aFileName)
{
var file;
try
{
  var regexpStr = "<LINK[^>]+href\s*=\s*[\'\"]" + aSrcUrl + "[^>]*>";
  this.regexp = new RegExp(regexpStr, "gi");
  file = contentDir.clone();
  file.append(aFileName);
  this.fileContent = "";
  var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"]
	.createInstance(Components.interfaces.nsIFileInputStream);
  var sstream = Components.classes["@mozilla.org/scriptableinputstream;1"]
    .createInstance(Components.interfaces.nsIScriptableInputStream);
  fstream.init(file, -1, 0, 0);
  sstream.init(fstream); 
  var str = sstream.read(4096);
  while (str.length > 0) {
    this.fileContent += str;
    str = sstream.read(4096);
  }
  sstream.close();
  fstream.close();
  this.fileContent = "<SCRIPT language=\"javascript\">\r\n" + this.fileContent + "\r\n</SCRIPT>\r\n";
}
catch (e)
{
debugMsg(e);
if (file)
  debugMsg(file.path);
}
}

CombatsCssIncludeFix.prototype.apply = function(data)
{
  if (data.match(this.regexp))
  {
    var ret = data.replace(this.regexp, this.fileContent);
    return ret;
  }
  return data;
};


//CombatsFixCollection class
function CombatsFixCollection() {
  this.fixes = [];
};

CombatsFixCollection.prototype.add = function(fix)
{
  this.fixes[this.fixes.length] = fix;
}

CombatsFixCollection.prototype.apply = function(data)
{
  for (var fixNo = 0; fixNo < this.fixes.length; fixNo++)
  {
    data = this.fixes[fixNo].apply(data);
  }
  return data;
};

var fixesCollection = new CombatsFixCollection();
fixesCollection.add(new CombatsRegExpFix(/cursor:\s*hand/gi, "cursor: pointer"));
fixesCollection.add(new CombatsRegExpFix(/padding:\s*(\d+)\s*\,\s*(\d+)\s*\,\s*(\d+)\s*\,\s*(\d+)/gi, "padding: $1 $2 $3 $4"));
fixesCollection.add(new CombatsRegExpFix(/function\s*top\.(\w+)\s*\(/gi, "top.$1 = function("));
fixesCollection.add(new CombatsRegExpFix(/border\-width:\s*(\d+)px\s*solid/gi, "border: $1px solid"));
fixesCollection.add(new CombatsRegExpFix(/document\.all\.item;/gi, ""));
fixesCollection.add(new CombatsRegExpFix(/\(\s*!document\.all\s*\)/gi, "(false)"));
fixesCollection.add(new CombatsRegExpFix(/oTopStyle\.insertRule\( oStyleSheet\.cssRules\[ i \]\.selectorText \+/gi, "oTopStyle.insertRule( oStyleSheet.cssRules[i].cssText, (oStyleSheet.cssRules[i].type===3) ? 0 : oTopStyle.cssRules.length );"));
fixesCollection.add(new CombatsRegExpFix(/\"\{\" \+ oStyleSheet\.cssRules\[ i \]\.style\.cssText \+ \"\}\"\, 0 \);/gi, ""));
fixesCollection.add(new CombatsRegExpFix(/\/\/\s*alert\(\s*e\s*\);/gi, "alert(e);"));
fixesCollection.add(new CombatsRegExpFix(/mailspan\.innerHTML/gi, "document.getElementById('mailspan').innerHTML"));
fixesCollection.add(new CombatsRegExpFix(/function gotoForKeyDown\(\) \{/, "function gotoForKeyDown(e) {"));
fixesCollection.add(new CombatsRegExpFix(/c \= window\.event\.keyCode;/, "c = (e && e.keyCode) ? e.keyCode : 0;"));
fixesCollection.add(new CombatsRegExpFix(/function\s+Down\(\)/gi, "function Down(e)"));
fixesCollection.add(new CombatsRegExpFix(/\{\s*top\.CtrlPress\s*\=\s*window\.event\.ctrlKey\s*\}/gi, "{top.CtrlPress=(e && e.ctrlKey) ? e.ctrlKey : false}"));
fixesCollection.add(new CombatsRegExpFix(/\.navigate\(/gi, ".document.location = ("));
fixesCollection.add(new CombatsRegExpFix(/\(\s*window\.ActiveXObject\s*\)/gi, "(window.XMLHttpRequest)"));
fixesCollection.add(new CombatsRegExpFix(/new\s*ActiveXObject\(\s*\"Microsoft\.XMLHTTP\"\s*\)/gi, "new XMLHttpRequest()"));
fixesCollection.add(new CombatsRegExpFix(/new\s*ActiveXObject\(\s*\"MSxml2\.XMLHTTP\"\s*\)/gi, "new XMLHttpRequest()"));
fixesCollection.add(new CombatsRegExpFix(/room\.innerHTML/gi, "document.getElementById('room').innerHTML"));
fixesCollection.add(new CombatsRegExpFix(/pager\.innerHTML/gi, "document.getElementById('pager').innerHTML"));
fixesCollection.add(new CombatsRegExpFix(/users\.innerHTML/gi, "document.getElementById('users').innerHTML"));
fixesCollection.add(new CombatsRegExpFix(/ione\.innerHTML/gi, "document.getElementById('ione').innerHTML"));
fixesCollection.add(new CombatsRegExpFix(/innerspan\.innerHTML/gi, "document.getElementById('innerspan').innerHTML"));
fixesCollection.add(new CombatsRegExpFix(/innerdata\.innerHTML/gi, "document.getElementById('innerdata').innerHTML"));
fixesCollection.add(new CombatsRegExpFix(/citydiv\.innerHTML/gi, "document.getElementById('citydiv').innerHTML"));
fixesCollection.add(new CombatsRegExpFix(/\.frames\[\'bottom\'\]\.F1/gi, ".frames['bottom'].document.forms['F1']"));

fixesCollection.add(new CombatsRegExpFix(/function fastshow\(dsc\, dx\, dy\)/gi, "function fastshow(windowevent, dsc, dx, dy)"));
fixesCollection.add(new CombatsRegExpFix(/function fastshow\s*\(content\)\s*{/gi, "function fastshow(windowevent, content) { window.event = windowevent;"));

fixesCollection.add(new CombatsRegExpFix(/\{ top\.fullfastshow\(document\, mmoves\, window\.event\, dsc\, dx\, dy\); \}/gi, "{ top.fullfastshow(document, document.getElementById('mmoves'), windowevent, dsc, dx, dy); }"));
fixesCollection.add(new CombatsRegExpFix(/(\=\s*[\"\'](javascript\:)?fastshow\()/gi, "$1event, "));
fixesCollection.add(new CombatsRegExpFix(/{ top\.fullhideshow\(mmoves\); \}/gi, "{ top.fullhideshow(document.getElementById('mmoves')); }"));

fixesCollection.add(new CombatsRegExpFix(/function gfastshow\(dsc\, dx\, dy\)/gi, "function gfastshow(windowevent, dsc, dx, dy)"));
fixesCollection.add(new CombatsRegExpFix(/\{ top\.fullfastshow\(document\, mmoves(\d+)\, window\.event\, dsc\, dx\, dy\); \}/gi, "{ top.fullfastshow(document, document.getElementById('mmoves$1'), windowevent, dsc, dx, dy); }"));
fixesCollection.add(new CombatsRegExpFix(/\=\'gfastshow\(/gi, "='gfastshow(event, "));
fixesCollection.add(new CombatsRegExpFix(/\=\"gfastshow\(/gi, "=\"gfastshow(event, "));
fixesCollection.add(new CombatsRegExpFix(/{ top\.fullhideshow\(mmoves(\d+)\); \}/gi, "{ top.fullhideshow(document.getElementById('mmoves$1')); }"));

fixesCollection.add(new CombatsRegExpFix(/\.srcElement/gi, ".target"));

fixesCollection.add(new CombatsRegExpFix(/document\.F1\.all/gi, "document.forms['F1'].elements"));
fixesCollection.add(new CombatsRegExpFix(/\<span id\=\"menu(\d+)\" style\=\"visibility\:hidden/gi, "<span id=\"menu$1\" style=\"display:none"));
fixesCollection.add(new CombatsRegExpFix(/document\.getElementById\(\'menu\'\+tblname\)\.style\.visibility\s*\=\s*\'visible\';/gi, "document.getElementById('menu'+tblname).style.display = '';"));
fixesCollection.add(new CombatsRegExpFix(/document\.getElementById\(\'menu\'\+i\)\.style\.visibility\s*\=\s*\'hidden\';/gi, "document.getElementById('menu'+i).style.display= 'none';"));

fixesCollection.add(new CombatsRegExpFix(/=\"smiles\(\)\"/gi, "=\"smiles(event)\""));
fixesCollection.add(new CombatsRegExpFix(/function\s+smiles\(\)/gi, "function smiles(event)"));
fixesCollection.add(new CombatsRegExpFix(/window\.showModelessDialog\(\"smiles\.html\"\, window\, sFeatures\);/gi, "window.open('smiles.html', 'SmilesWnd', 'top=' + x + ',left=' + y + ',height=110,width=400,scrollbars=yes,titlebar=no,resizable=yes,status=no,toolbar=no,menubar=no,location=no');"));
fixesCollection.add(new CombatsRegExpFix(/var\s+sData\s*=\s*dialogArguments;/gi, "var sData=window.opener.document.forms;"));

fixesCollection.add(new CombatsRegExpFix(/\=\'top\.gfastshow\((.+)?\, window\)/gi, "='top.gfastshow($1, {event: event})"));
fixesCollection.add(new CombatsRegExpFix(/this.filters.Gray.Enabled=false/gi, " "));
fixesCollection.add(new CombatsRegExpFix(/this.filters.Gray.Enabled=true/gi, " "));
fixesCollection.add(new CombatsRegExpFix(/r\.filters\.Gray\.Enabled=false;/gi, " "));
fixesCollection.add(new CombatsRegExpFix(/r\.filters\.Gray\.Enabled=true;/gi, " "));
fixesCollection.add(new CombatsRegExpFix(/r\.filters\.Glow\.Enabled=false;/gi, " "));
fixesCollection.add(new CombatsRegExpFix(/r\.filters\.Glow\.Enabled=true;/gi, " "));

fixesCollection.add(new CombatsRegExpFix(/onmouseover\=\"this\.filters\.Gray\.Enabled\=false\"/gi, ""));
fixesCollection.add(new CombatsRegExpFix(/onmouseout\=\"this\.filters\.Gray\.Enabled\=true\"/gi, ""));

fixesCollection.add(new CombatsRegExpFix(/(<table[^>]+>)\s*<tr>\s*(<form[^>]+>)/gi, "$2$1<tr>"));
fixesCollection.add(new CombatsRegExpFix(/<\/TR>\s*<\/FORM>\s*<\/TABLE>/gi, "</TR></TABLE></FORM>"));

fixesCollection.add(new CombatsRegExpFix(/background\-image\:\s*url\(\s*\)/gi, ""));
fixesCollection.add(new CombatsRegExpFix(/style\=\'position\: absolute; left\: 5[\s\w]*/gi, "style=\'position: absolute"));
fixesCollection.add(new CombatsRegExpFix(/<div style=\"position\:absolute; right\:0px; top\:0px; width\:1px; height\:1px; z-index\:101; overflow\:visible;\">/gi, 
  "<div style=\"position:absolute; right:0px; top:0px; width:92px; height:1px; z-index:101; overflow:visible;\">"));
  
fixesCollection.add(new CombatsRegExpFix(/\'position\: absolute; top\: 1; left\: 5/gi, "'position: absolute"));
fixesCollection.add(new CombatsRegExpFix(/\"position\: absolute; top\: 1; left\: 5/gi, "\"position: absolute"));

fixesCollection.add(new CombatsRegExpFix(/^(<TD[^>]*>\s*)<HTML>/gim, "$1"));

fixesCollection.add(new CombatsRegExpFix(/req\.send\(\)/gi, "req.send(null)"));
fixesCollection.add(new CombatsRegExpFix(/Math\.toHex\(/gi, "top.MathToHex("));
fixesCollection.add(new CombatsRegExpFix(/<param name\=\"movie\" value\=\"(http\:\/\/img\.combats\.com\/i\/flash\/clock.swf[^\"]+)\" \/>/gi, "<embed src=\"$1\" quality=\"high\" width=\"70\" height=\"26\" name=\"Clocks\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\"></EMBED>"));
fixesCollection.add(new CombatsRegExpFix(/<param name\=movie value\=\"(\/i\/flash\/psw2.swf[^\"]+)\">/gi, "<embed src=\"$1\" quality=\"high\" width=\"600\" height=\"250\" name=\"Psw2\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\"></EMBED>"));
fixesCollection.add(new CombatsRegExpFix(/document\.getElementById\(\'dd\'\)\.add\(/gi, "document.getElementById('dd').appendChild("));
fixesCollection.add(new CombatsRegExpFix(/<div style\=\"position\:absolute; left\:0px; top\:0px; width\:120px; height\:220px; z-index\:(\d+)\">\s*<img/gi, "<div style=\"position:absolute; left:0px; top:0px; width:1px; height:1px; z-index:$1\"><img"));

fixesCollection.add(new CombatsRegExpFix(/(<style\s+id\=\'mst1\'\s*>)/gi, "$1 div.TabFrame {left:0;position:absolute;top:0;width:100%}"));
fixesCollection.add(new CombatsRegExpFix(/(for\s*\(\s*var\s+j\s*=\s*0;\s*j\s*<\s*oNode\.childNodes\.length;\s*j\+\+\s*\))/gi, "$1 if (oNode.childNodes[j].nodeType === 1)"));
fixesCollection.add(new CombatsRegExpFix(/eEvent.button\s*==\s*1/gi, "eEvent.button==0"));
fixesCollection.add(new CombatsRegExpFix(/document\.URL\s*=\s*(\w+);/gi, "document.location = $1"));
fixesCollection.add(new CombatsRegExpFix(/<input type=\"text\" name=\"text\"/gi, "<input type=\"text\" name=\"text\" style=\"margin-bottom:6px;\""));
fixesCollection.add(new CombatsRegExpFix(/document.F1.text.width\s*=\s*size;/gi, "document.forms['F1'].text.style.width=size+'px';"));
fixesCollection.add(new CombatsRegExpFix(/document.F1.text.width\s*=\s*size;/gi, "document.forms['F1'].text.style.width=size+'px';"));
fixesCollection.add(new CombatsRegExpFix(/\@import url\(\"http\:\/\/img\.combats\.com\/css\/Tab\/style\.css\"\);/gi, ""));
fixesCollection.add(new CombatsRegExpFix(/^(DIV\.TabTabsLayer\s*\{[^\}]+)padding-right:\s*\d+px;/gim, "$1right: 15px;"));

fixesCollection.add(new CombatsRegExpFix(/\@import url\(\"http\:\/\/img\.combats\.com\/css\/User\/Battle\/style4\.css\"\);/gi, ""));
fixesCollection.add(new CombatsRegExpFix(/^(DIV\.UserBattleRadio\s*\{[^\}]+)top:\s*\d+px;/gim, "$1top: -10px;"));

fixesCollection.add(new CombatsRegExpFix(/(<IMG style=\"filter\:gray\(\)\, Alpha\(Opacity\=\'70\'\)\;)/gi, "$1 opacity: 0.3;"));
fixesCollection.add(new CombatsRegExpFix(/style\=\"background\:;color\:\#EFE8CB;\"/gi, "style=\"background:#000000;color:#EFE8CB;\""));
fixesCollection.add(new CombatsRegExpFix(/\"colorPick\.on\(/gi, "\"colorPick.on(event, "));
fixesCollection.add(new CombatsRegExpFix(/\"smiles\(/gi, "\"smiles(event, "));
fixesCollection.add(new CombatsRegExpFix(/\'colorPick\.on\(/gi, "'colorPick.on(event, "));
fixesCollection.add(new CombatsRegExpFix(/\'smiles\(/gi, "'smiles(event, "));
fixesCollection.add(new CombatsRegExpFix(/\"javascript\:add2form\(dialogArguments\./gi, "\"javascript:add2form(window.opener.document.forms."));

fixesCollection.add(new CombatsRegExpFix(/(=\s*\"(javascript:)?[\w\_]+panel\()/gi, "$1event, "));
fixesCollection.add(new CombatsRegExpFix(/(=\s*\"(javascript:)?user\_info\()/gi, "$1event, "));
fixesCollection.add(new CombatsRegExpFix(/(=\s*\"(javascript:)?window\_add\_friend\()/gi, "$1event, "));
fixesCollection.add(new CombatsRegExpFix(/(=\s*\"(javascript:)?window\_new\_group\()/gi, "$1event, "));
fixesCollection.add(new CombatsRegExpFix(/(=\s*\"(javascript:)?window\_addUserToGroup\()/gi, "$1event, "));
fixesCollection.add(new CombatsRegExpFix(/(=\s*\"(javascript:)?com\_info\()/gi, "$1event, "));
fixesCollection.add(new CombatsRegExpFix(/(=\s*\"(javascript:)?winpasswd\_panel\()/gi, "$1event, "));
fixesCollection.add(new CombatsRegExpFix(/(=\s*\"(javascript:)?copy\_message\_panel\()/gi, "$1event, "));
fixesCollection.add(new CombatsRegExpFix(/(=\s*\"(javascript:)?in\_top\_panel\()/gi, "$1event, "));
fixesCollection.add(new CombatsRegExpFix(/(=\s*\"(javascript:)?out\_top\_panel\()/gi, "$1event, "));
fixesCollection.add(new CombatsRegExpFix(/(=\s*\"(javascript:)?top\_vote\_panel\()/gi, "$1event, "));
fixesCollection.add(new CombatsRegExpFix(/(=\s*\"(javascript:)?join\_top\_panel\()/gi, "$1event, "));

fixesCollection.add(new CombatsJsIncludeFix("http\\:\\/\\/img\\.combats\\.ru\\/i\\/js\\/dialog_028_ru.js", "dialog_028_ru.js"));
fixesCollection.add(new CombatsJsIncludeFix("http\\:\\/\\/img\\.combats\\.com\\/i\\/js\\/dialog_026_ru.js", "dialog_026_ru.js"));
fixesCollection.add(new CombatsJsIncludeFix("http\\:\\/\\/img\\.combats\\.ru\\/i\\/js\\/dialog_026_ru.js", "dialog_026_ru.js"));
fixesCollection.add(new CombatsJsIncludeFix("http\\:\\/\\/img\\.combats\\.com\\/i\\/js\\/dialog_028_ru.js", "dialog_028_ru.js"));
fixesCollection.add(new CombatsJsIncludeFix("http\\:\\/\\/img\\.combats\\.ru\\/i\\/js\\/dialog_028_ru.js", "dialog_028_ru.js"));
fixesCollection.add(new CombatsJsIncludeFix("http\\:\\/\\/img\\.combats\\.com\\/i\\/js\\/main_093_ru.js", "main_093_ru.js"));
fixesCollection.add(new CombatsJsIncludeFix("http\\:\\/\\/img\\.combats\\.ru\\/i\\/js\\/main_093_ru.js", "main_093_ru.js"));
fixesCollection.add(new CombatsJsIncludeFix("http\\:\\/\\/img\\.combats\\.com\\/i\\/ch2.91.js", "ch2.91.js"));
fixesCollection.add(new CombatsJsIncludeFix("http\\:\\/\\/img\\.combats\\.ru\\/i\\/ch2.91.js", "ch2.91.js"));
fixesCollection.add(new CombatsJsIncludeFix("http\\:\\/\\/scrolls\\.combats\\.com\\/js\\/common\\.js\\?ver\\=085", "scrollscommon.js"));
fixesCollection.add(new CombatsJsIncludeFix("\\/js\\/common\\.js\\?ver\\=085", "scrollscommon.js"));
fixesCollection.add(new CombatsJsIncludeFix("\\/js\\/common\\.js", "scrollscommon.js"));
fixesCollection.add(new CombatsJsIncludeFix("http\\:\\/\\/img\\.combats\\.com\\/i\\/move\\/dungeon8\\.js", "dungeon8.js"));
fixesCollection.add(new CombatsJsIncludeFix("http\\:\\/\\/img\\.combats\\.com\\/i\\/js\\/inf\\.0\\.102\\.js", "inf.0.102.js"));
fixesCollection.add(new CombatsJsIncludeFix("http\\:\\/\\/img\\.combats\\.com\\/i\\/js\\/inf\\.0\\.102\\.js", "inf.0.102.js"));
fixesCollection.add(new CombatsJsIncludeFix("http\\:\\/\\/img\\.combats\\.com\\/i\\/sl2\\.27\\.js", "sl2.27.js"));
fixesCollection.add(new CombatsJsIncludeFix("http\\:\\/\\/img\\.combats\\.com\\/i\\/selectlogin\\.js", "selectlogin.js"));

 
// This is RequestObserver's method 
CombatsStreamConvertor.prototype.onStopRequest = function(aRequest, aContext, 
                 aStatusCode) { 
  //debugMsg("onStopRequest enter");
  
    this.data = fixesCollection.apply(this.data);
    debugMsg("fixes applyied");
  
         var sis = Components.classes["@mozilla.org/io/string-input-stream;1"] 
                         .createInstance(Components.interfaces.nsIStringInputStream); 
         sis.setData(this.data, this.data.length); 
  
         // Pass the data to the main content listener 
         //this.channel.contentCharset = this.charset; 
         this.listener.onDataAvailable(this.channel, aContext, sis, 0, 
                         this.data.length); 
         this.listener.onStopRequest(this.channel, aContext, aStatusCode); 
}; 
  
// nsIStreamListener methods 
CombatsStreamConvertor.prototype.onDataAvailable = function(aRequest, aContext, 
                aInputStream, aOffset, aCount) { 
  //debugMsg("onDataAvailable enter");
  
        var si = Components.classes["@mozilla.org/scriptableinputstream;1"] 
                        .createInstance(); 
        si = si.QueryInterface(Components.interfaces.nsIScriptableInputStream); 
        si.init(aInputStream); 
        var data = si.read(aCount); 
        this.data += data; 
}; 
 
// nsIStreamConverter methods 
// old name (before bug 242184)... 
CombatsStreamConvertor.prototype.AsyncConvertData = function(aFromType, aToType, 
                aListener, aCtxt) { 
  //debugMsg("AsyncConvertData enter");
	this.asyncConvertData(aFromType, aToType, aListener, aCtxt); 
}; 
 
// renamed to... 
CombatsStreamConvertor.prototype.asyncConvertData = function(aFromType, aToType, 
                aListener, aCtxt) { 
  //debugMsg("asyncConvertData enter");
        this.listener = aListener; 
}; 
 
// Old name (before bug 242184): 
CombatsStreamConvertor.prototype.Convert = function(aFromStream, aFromType, 
                aToType, aCtxt) { 
  //debugMsg("Convert enter");
        return this.convert(aFromStream, aFromType, aToType, aCtxt); 
}; 
 
// renamed to... 
CombatsStreamConvertor.prototype.convert = function(aFromStream, aFromType, 
                aToType, aCtxt) { 
  //debugMsg("convert enter");
        return aFromStream; 
}; 

/* stream converter factory object (CombatsStreamConvertor) */ 
var CombatsStreamConvertorFactory = new Object(); 
 
CombatsStreamConvertorFactory.createInstance = function(outer, iid) { 
  //debugMsg("createInstance");
        if (outer != null) 
                throw Components.results.NS_ERROR_NO_AGGREGATION; 
 
        if (iid.equals(Components.interfaces.nsISupports) 
                        || iid.equals(Components.interfaces.nsIStreamConverter) 
                        || iid.equals(Components.interfaces.nsIStreamListener) 
                        || iid.equals(Components.interfaces.nsIRequestObserver)) { 
                 return new CombatsStreamConvertor(); 
        } 
        throw Components.results.NS_ERROR_INVALID_ARG; 
}; 

var CombatsStreamConvertorModule = new Object(); 
 
CombatsStreamConvertorModule.registerSelf = function(compMgr, fileSpec, location, type) { 
  //debugMsg("registerSelf");
 
        var compMgr = compMgr 
                        .QueryInterface(Components.interfaces.nsIComponentRegistrar); 
 
        compMgr.registerFactoryLocation(CLASS_ID, 
                        "Combats Online Game Stream Converter", 
                        CONTRACT_ID, fileSpec, location, type); 
 
        var catman = Components.classes["@mozilla.org/categorymanager;1"] 
                        .getService(Components.interfaces.nsICategoryManager); 
        catman.addCategoryEntry("@mozilla.org/streamconv;1", 
                        CONVERSION, 
                        "com.combats.html to HTML stream converter", true, true); 
 
}; 
 
CombatsStreamConvertorModule.unregisterSelf = function(compMgr, fileSpec, location) { 
  //debugMsg("unregisterSelf");
}; 
 
CombatsStreamConvertorModule.getClassObject = function(compMgr, cid, iid) { 
  //debugMsg("getClassObject");
 
        if (cid.equals(CLASS_ID)) 
                return CombatsStreamConvertorFactory; 
 
        if (!iid.equals(Components.interfaces.nsIFactory)) 
                throw Components.results.NS_ERROR_NOT_IMPLEMENTED; 
 
        throw Components.results.NS_ERROR_NO_INTERFACE; 
 
}; 
 
CombatsStreamConvertorModule.canUnload = function(compMgr) { 
  //debugMsg("canUnload");
        return true; 
}; 

 
/* entrypoint */ 
function NSGetModule(compMgr, fileSpec) { 
  //debugMsg("NSGetModule");

        return CombatsStreamConvertorModule; 
} 
